package com.kasax.android.arch.webview.client;

import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.kasax.android.arch.utils.LogUtils;
import com.kasax.android.arch.webview.callback.WebChromeClientCallback;

public class InternalWebChromeClient extends WebChromeClient {

    private final WebChromeClientCallback callback;

    public InternalWebChromeClient(WebChromeClientCallback callback) {
        this.callback = callback;
    }

    @Override
    public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
        LogUtils.d("WebViewClient onJsConfirm url: " + url + " , message: " + message + " , result: " + result);
        return super.onJsConfirm(view, url, message, result);
    }

    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
        LogUtils.d("WebViewClient onJsPrompt url: " + url + " , message: " + message + " , result: " + result);
        return super.onJsPrompt(view, url, message, defaultValue, result);
    }

    @Override
    public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
        LogUtils.d("WebViewClient onJsBeforeUnload url: " + url + " , message: " + message + " , result: " + result);
        return super.onJsBeforeUnload(view, url, message, result);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        LogUtils.d("WebViewClient onJsAlert url: " + url + " , message: " + message + " , result: " + result);
        boolean fresult = super.onJsAlert(view, url, message, result);
        return fresult;
    }

    @Override
    public void onCloseWindow(WebView window) {
        super.onCloseWindow(window);
        LogUtils.d("WebViewClient onCloseWindow");
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        LogUtils.d("WebViewClient onShowCustomView");
    }

    @Override
    public void onPermissionRequest(PermissionRequest request) {
        super.onPermissionRequest(request);
        LogUtils.d("WebViewClient onPermissionRequest");
    }

    @Override
    public void onPermissionRequestCanceled(PermissionRequest request) {
        super.onPermissionRequestCanceled(request);
        LogUtils.d("WebViewClient onPermissionRequestCanceled");
    }

    @Override
    public View getVideoLoadingProgressView() {
        LogUtils.d("WebViewClient getVideoLoadingProgressView");
        return super.getVideoLoadingProgressView();
    }

    @Override
    public Bitmap getDefaultVideoPoster() {
        LogUtils.d("WebViewClient getDefaultVideoPoster");
        return super.getDefaultVideoPoster();
    }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        LogUtils.d("WebViewClient onShowFileChooser");
        return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
    }

    @Override
    public void getVisitedHistory(ValueCallback<String[]> callback) {
        super.getVisitedHistory(callback);
        LogUtils.d("WebViewClient getVisitedHistory");
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);

        LogUtils.d("WebViewClient onProgressChanged newProgress: " + newProgress);
        if (callback != null) {
            callback.onProgressChanged(newProgress);
        }
        if (newProgress == 100) {
            // 网页加载完成
            view.getSettings().setBlockNetworkImage(false);
        } else {
            // 网页加载中
        }

    }


}